﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Text;

namespace MShop.Core.Domain
{

    [MetadataType(typeof(Trade_Validation))]
    public partial class Trade
    {
        public Trade()
        {
            TradeId = DateTime.Now.ToString("yyyyMMddMMhhssfff");
            Created = DateTime.Now;
            this.Payment = 0.00M;
            this.DisCount = 0.00M;
          //  this.DeliveryPeriodType = 1;
            HasInvoice = false;
        }
        /// <summary>
        /// 计算后的商品总额=商品总额+邮费-折扣
        /// </summary>
        public Decimal TotalSum
        {
            get
            {
                return TotalFee + PostFee+DisCount;
            }
        }

        /// <summary>
        /// 计算后的商品总积分
        /// </summary>
        public int CacluateTotalPoint
        {
            get
            {
                int totalPoint = 0;
                foreach (var item in Order)
                {
                    totalPoint += item.OrderPoint;
                }
                return totalPoint;
            }
        }
        public string Subject
        {
            get
            {
               
                if (this.Order.Count == 1)
                {
                    return Order.ToList()[0].Product.ProductName;
                }
                else
                {
                    return Order.ToList()[0].Product.ProductName + "...";
                }                

               
            }
        }

        public string Body
        {
            get
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("商品包括：");
                foreach (var item in this.Order)
                {
                    sb.Append(item.Product.ProductName + ",");
                }

                return sb.Remove(sb.Length - 1, 1).ToString();
            }
        }

        public string TradeProductName
        {
            get
            {
                StringBuilder sb =new  StringBuilder();
                foreach (var item in this.Order)
                {
                    string str = string.Format("<a target='_blank' href='/product/item/{2}' title='{1}' ><img class='thumb' src='{0}' alt='{1}'  /></a>", item.Product.ThumbImageUrl, item.Product.ProductName,item.ProductId);
                    sb.Append(str);
                }

                return sb.ToString();
            }
        }

        public TradeStatus GetTradeStatus(int status)
        {
            return (TradeStatus)status;
        }
    }

    internal partial class Trade_Validation
    {

        [DisplayName("交易编号")]
        [System.ComponentModel.DataAnnotations.Required(ErrorMessage = "交易编号不能为空")]
        public string TradeId { get; set; }

        [DisplayName("支付方式")]
        [Range(1,9999999999,ErrorMessage="支付方式不能为空")]
        [System.ComponentModel.DataAnnotations.Required(ErrorMessage = "支付方式不能为空")]
        public int PayId { get; set; }


   
        [DisplayName("配送方式")]
        [Range(1,99999999999,ErrorMessage="配送方式不能为空")]
        [System.ComponentModel.DataAnnotations.Required(ErrorMessage = "配送方式不能为空")]
        public int DistributionId { get; set; }       

        [DisplayName("交易状态")]    
        public int TradeStatus { get; set; }

        [DisplayName("折扣")]
        public int DisCount { get; set; }  

        [DisplayName("移动电话")]
        public string Mobile { get; set; }

        [Foolproof.RequiredIfEmpty("Mobile", ErrorMessage = "固定电话,移动电话必须要填写其中一个")]
        [DisplayName("固定电话")]
        public string Tel { get; set; }

        [DisplayName("积分")]
        public int TotalPoint { get; set; }

        [UIHint("AreaControl")]
        [DisplayName("地区")]
        public int AreaId { get; set; }

        [Required(ErrorMessage = "联系人姓名不能为空")]
        [DisplayName("联系人姓名")]
        public string ReceiverName { get; set; }
       

        [Required(ErrorMessage = "邮政编码不能为空")]
        [DisplayName("邮政编码")]
        public string PostCode { get; set; }

        [Required(ErrorMessage = "固定地址不能为空")]
        [StringLength(50,ErrorMessage="固定地址太短或者太长",MinimumLength=5)]
        [DisplayName("固定地址")]
        public string Location { get; set; }
         
       
        [DisplayName("会员名称")]
        public Guid UserId { get; set; }

        [DisplayName("支付金额")]
        public decimal Payment { get; set; }

        [DisplayName("商品金额")]
        public decimal TotalFee { get; set; }


        [DisplayName("邮费金额")]
        public decimal PostFee { get; set; }

        [DisplayName("支付时间")]
        public DateTime PayTime { get; set; }
         
        [DisplayName("修改时间")]
        public DateTime Modified { get; set; }


        [DisplayName(" 交易结束时间")]
        public DateTime EndTime { get; set; }

        [DisplayName("创建时间")]
        public DateTime Created { get; set; }

        [DisplayName("买家备注")]
        public string BuyerMemo { get; set; }


        [DisplayName("卖家备注")]
        public string SellerMemo { get; set; }

        [DisplayName("是否需要发票")]
        public bool HasInvoice { get; set; }


        [DisplayName("发票抬头")]
        public string InvoiceName { get; set; }

        [Required(ErrorMessage = "请选择配送时间")]
        [DisplayName("配送时间")]
        public decimal DeliveryPeriodType { get; set; }

    }
}